#include <string.h>
#include "HChar.h"
#include "JAssert.h"

HChar::HChar(void)
{
}

HChar::HChar(JTexture *tex,float width, float height)
{
	int i;
	width -= 1;
	height -= 1;

	mDirection = D_DOWN;

	mDownSprite = new JSprite(tex,0,0,width,height);
	for(i = width+1;i < 128;i += width+1 )
	{
		mDownSprite->AddFrame(tex,i,0,width,height);
	}
	
	mLeftSprite = new JSprite(tex,0,height+1,width,height);
	for(i = width+1;i < 128;i += width+1 )
	{
		mLeftSprite->AddFrame(tex,i,height+1,width,height);
	}

	mRightSprite = new JSprite(tex,0,(height+1)*2,width,height);
	for(i = width+1;i < 128;i += width+1 )
	{
		mRightSprite->AddFrame(tex,i,(height+1)*2,width,height);
	}
	
	mUpSprite = new JSprite(tex,0,(height+1)*3,width,height);
	for(i = width+1;i < 128;i += width+1 )
	{
		mUpSprite->AddFrame(tex,i,(height+1)*3,width,height);
	}

	
	mUpSprite->SetDuration(10);
	mUpSprite->StartAnimation();
	
	mDownSprite->SetDuration(10);
	mDownSprite->StartAnimation();
	
	mLeftSprite->SetDuration(10);
	mLeftSprite->StartAnimation();

	mRightSprite->SetDuration(10);
	mRightSprite->StartAnimation();

	mDstX = 0.0f;
	mDstY = 0.0f;
}
HChar::~HChar(void)
{
	delete mUpSprite;
	delete mDownSprite;
	delete mLeftSprite;
	delete mRightSprite;

}


void HChar::SetName(char *name)
{
	strcpy(mName,name);	
}
void HChar::SetDesc(char *desc)
{
	strcpy(mDesc,desc);
}
void HChar::SetLevel(u8 level)
{
	mLevel = level;
}
void HChar::SetExp(u8 exp)
{
	mExp = exp;
}
void HChar::SetMaxhp(u32 maxhp)
{
	mMaxhp = maxhp;
}
void HChar::SetHp(u32 hp)
{
	mHp = hp;
}
void HChar::SetStr(u32 str)
{
	mStr = str;
}
void HChar::SetSkl(u32 skl)
{
	mSkl = skl;
}
void HChar::SetSpd(u32 spd)
{
	mSpd = spd;
}
void HChar::SetLck(u32 lck)
{
	mLck = lck;
}
void HChar::SetDef(u32 def)
{
	mDef = def;
}
void HChar::SetMdf(u32 mdf)
{
	mMdf = mdf;
}
void HChar::SetMov(u32 mov)
{
	mMov = mov;
}

char* HChar::GetName()
{
	return mName;
}
char* HChar::GetDesc()
{
	return mDesc;
}
u8	 HChar::GetLevel()
{
	return mLevel;
}
u8   HChar::GetExp()
{
	return mExp;
}
u32  HChar::GetMaxhp()
{
	return mMaxhp;
}
u32  HChar::GetHp()
{
	return mHp;
}
u32  HChar::GetStr()
{
	return mStr;
}
u32  HChar::GetSkl()
{
	return mSkl;
}
u32  HChar::GetSpd()
{
	return mSpd;
}
u32  HChar::GetLck()
{
	return mLck;
}
u32  HChar::GetDef()
{
	return mDef;
}
u32  HChar::GetMdf()
{
	return mMdf;
}
u32  HChar::GetMov()
{
	return mMov;
}



////////////////////////////////////////////

void HChar::SetHead(JQuad	 *head)
{
	mHead = head;
}
JSprite* HChar::GetUpSprite()
{
	return mUpSprite;
}
JSprite* HChar::GetDownSprite()
{
	return mDownSprite;
}
JSprite* HChar::GetLeftSprite()
{
	return mLeftSprite;
}
JSprite* HChar::GetRightSprite()
{
	return mRightSprite;
}
JQuad*   HChar::GetHead()
{
	return mHead;
}

void HChar::SetDirection(DIR dir)
{
	mDirection = dir;
}

DIR HChar::GetDirection()
{
	return mDirection;
}

JSprite* HChar::GetSprite()
{
	JSprite* sp;
	switch (mDirection)
	{
	case D_UP:
		sp = mUpSprite;
		break;
	case D_DOWN:
		sp = mDownSprite;
		break;
	case D_LEFT:
		sp = mLeftSprite;
		break;
	case D_RIGHT:
		sp = mRightSprite;
		break;
	}
	return sp;
}

bool HChar::Move()
{
	float x = mDstX;
	float y = mDstY;


	JSprite *sp = this->GetSprite();

	if(x != sp->GetX())
	{
		JASSERT(y == 0);

		if (x == sp->GetX()) return false;
		if (x > sp->GetX())	
		{
			mDirection = D_RIGHT;
			mUpSprite->Move(1,0);
			mDownSprite->Move(1,0);
			mLeftSprite->Move(1,0);
			mRightSprite->Move(1,0);
		}
		else
		{
			mDirection = D_LEFT;
			mUpSprite->Move(-1,0);
			mDownSprite->Move(-1,0);
			mLeftSprite->Move(-1,0);
			mRightSprite->Move(-1,0);
		}
	}
	else
	{

		if (y == sp->GetY()) return false;
		if (y > sp->GetY())	
		{
			mDirection = D_DOWN;
			mUpSprite->Move(0,1);
			mDownSprite->Move(0,1);
			mLeftSprite->Move(0,1);
			mRightSprite->Move(0,1);
		}
		else
		{
			mDirection = D_UP;
			mUpSprite->Move(0,-1);
			mDownSprite->Move(0,-1);
			mLeftSprite->Move(0,-1);
			mRightSprite->Move(0,-1);
		}
	}

	return true;
}

void HChar::SetMoving(bool b)
{
	mIsMoving = b;
}


bool HChar::IsMoving()
{
	return mIsMoving;
}

void HChar::SetPosition(float x,float y)
{
	mDstX = x;
	mDstY = y;
	
	mUpSprite->SetPosition(x,y);
	mDownSprite->SetPosition(x,y);
	mLeftSprite->SetPosition(x,y);
	mRightSprite->SetPosition(x,y);
}

void HChar::SetDst(float x,float y)
{
	mDstX = x;
	mDstY = y;

}

void HChar::Update(float dt)
{

	if(this->Move() == false)
		mIsMoving = false;
	else
		mIsMoving = true;

	this->GetSprite()->Update(dt);
}